{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading ../data/san+francisco,ca.csv...\n",
      "Loading ../data/new+york,ny.csv...\n",
      "Loading ../data/springfield,ma.csv...\n",
      "Loading ../data/boston,ma.csv...\n",
      "Loading ../data/springfield,il.csv...\n",
      "Loading ../data/albany,ny.csv...\n",
      "Loading ../data/los+angeles,ca.csv...\n",
      "Loading ../data/chicago,il.csv...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>max_temp</th>\n",
       "      <th>min_temp</th>\n",
       "      <th>precipMM</th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>San Francisco</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   max_temp  min_temp  precipMM           city state\n",
       "0        13         8       0.0  San Francisco    CA\n",
       "1        13         8       0.0  San Francisco    CA\n",
       "2        13         8       0.0  San Francisco    CA\n",
       "3        13         8       0.0  San Francisco    CA\n",
       "4        13         8       0.0  San Francisco    CA"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import glob\n",
    "\n",
    "all_dfs = []\n",
    "\n",
    "for one_filename in glob.glob('../data/*,*.csv'):\n",
    "    print(f'Loading {one_filename}...')\n",
    "    city, state = one_filename.removeprefix('../data/').removesuffix('.csv').split(',')\n",
    "    one_df = pd.read_csv(one_filename, \n",
    "                     usecols=[1, 2, 19],\n",
    "                     names=['max_temp', 'min_temp', 'precipMM'],\n",
    "                     header=0)\n",
    "    one_df['city'] = city.replace('+', ' ').title()\n",
    "    one_df['state'] = state.upper()\n",
    "    all_dfs.append(one_df)\n",
    "\n",
    "df = pd.concat(all_dfs)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>New York</td>\n",
       "      <td>NY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Boston</td>\n",
       "      <td>MA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          city state\n",
       "0     New York    NY\n",
       "0       Boston    MA\n",
       "0  Los Angeles    CA"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Which cities had, on at least 3 occasions, precipitation of 15 mm or more?\n",
    "\n",
    "# Technique 1: Using a function without extra arguments\n",
    "def has_multiple_readings_at_least(mini_df):\n",
    "    return mini_df.loc[\n",
    "        mini_df['precipMM'] >= 15,\n",
    "        'precipMM'\n",
    "        ].count() >= 3\n",
    "    \n",
    "(\n",
    "    df\n",
    "    .groupby(['city', 'state'])\n",
    "    .filter(has_multiple_readings_at_least)\n",
    "    [['city', 'state']]\n",
    "    .drop_duplicates()\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>New York</td>\n",
       "      <td>NY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Boston</td>\n",
       "      <td>MA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>CA</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          city state\n",
       "0     New York    NY\n",
       "0       Boston    MA\n",
       "0  Los Angeles    CA"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Technique 2: Using a function with extra arguments\n",
    "\n",
    "def has_multiple_readings_at_least(mini_df, min_mm, times):\n",
    "    return mini_df.loc[\n",
    "        mini_df['precipMM'] >= min_mm,\n",
    "        'precipMM'\n",
    "        ].count() >= times\n",
    "\n",
    "(\n",
    "    df\n",
    "    .groupby(['city', 'state'])\n",
    "    .filter(has_multiple_readings_at_least,\n",
    "            min_mm=15,\n",
    "            times=3)\n",
    "    [['city', 'state']]\n",
    "    .drop_duplicates()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>New York</td>\n",
       "      <td>NY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Boston</td>\n",
       "      <td>MA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Albany</td>\n",
       "      <td>NY</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       city state\n",
       "0  New York    NY\n",
       "0    Boston    MA\n",
       "0    Albany    NY"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find cities that had at least 3 measurements of 10 mm precipitation or more, when the temperature was below 0 Celsuius.\n",
    "\n",
    "def has_multiple_readings_at_least(mini_df, min_mm, times):\n",
    "    return mini_df.loc[\n",
    "        ((mini_df['precipMM'] >= min_mm) &\n",
    "         (mini_df['min_temp'] <= 0)),\n",
    "        'precipMM'\n",
    "        ].count() >= times\n",
    "\n",
    "(\n",
    "    df\n",
    "    .groupby(['city', 'state'])\n",
    "    .filter(has_multiple_readings_at_least, min_mm=10, times=3)\n",
    "    [['city', 'state']]\n",
    "    .drop_duplicates()\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For each precipitation measurement, calculate the proportion of that city's total precipitation.\n",
    "def proportion_of_city_precip(s):\n",
    "    return s / s.sum()\n",
    "\n",
    "df['precip_pct'] = (\n",
    "    df\n",
    "    .groupby('city')['precipMM']\n",
    "    .transform(proportion_of_city_precip)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "city           state\n",
       "Albany         NY       0.029228\n",
       "Boston         MA       0.048302\n",
       "Chicago        IL       0.057257\n",
       "Los Angeles    CA       0.059242\n",
       "New York       NY       0.055149\n",
       "San Francisco  CA       0.056509\n",
       "Springfield    IL       0.030977\n",
       "               MA       0.023459\n",
       "Name: precip_pct, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['city', 'state'])['precip_pct'].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
